#!/bin/sh

dmesg -c > /dev/null
sleep 20

grep_out=`dmesg -c | grep deauthenticated`
if [ -n "$grep_out" ]; then
    echo "deauthenticated found" > /tmp/wpa.log
    /duke/ap_up.sh
    exit
fi


grep_out=`iw dev wlan0 link | grep SSID`
if [ -n "$grep_out" ]; then
    echo "successed" > /tmp/wpa.log
    udhcpc -i wlan0
    cp /tmp/wpa.conf /duke/
    wifi_connected=`iw dev wlan0 link | grep SSID | awk '{print $2}'`; echo $wifi_connected > /tmp/wifi-connected.txt
    sleep 5
    curl -s --data "command=update_connected&value=$wifi_connected" http://heater.dukelec.com/net > /dev/null
    exit
fi

echo "SSID not found" > /tmp/wpa.log
/duke/ap_up.sh
